package com.controller;

import com.entity.Auction;
import com.service.AuctionService;
import com.vo.LayuiPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/auction")
public class AuctionController {
    @Autowired
    private AuctionService auctionService;

    @GetMapping("/getAuctionLog/{commId}")
    public LayuiPageVo<List<Auction>> getAuctionLog(@PathVariable String commId) {
        List<Auction> list = auctionService.getBidLog(commId);
        return new LayuiPageVo<List<Auction>>("成功", 0, 1000, list);
    }

    @PostMapping("/addAuction/{commId}/{amount}")
    public LayuiPageVo<List<Auction>> addAuction(@PathVariable String commId,
                               @PathVariable BigDecimal amount,
                               HttpSession session) {
        String userId = (String) session.getAttribute("userid");
        List<Auction> bidLog = auctionService.getBidLog(commId);
        BigDecimal bid = bidLog.get(0).getBid();
        if (amount.compareTo(bid) > 0){
            auctionService.addAuctionBid(userId,commId,amount);
            return new LayuiPageVo<List<Auction>>("成功", 0, 2000, null);
        }else {
            return new LayuiPageVo<List<Auction>>("出价低于拍卖价", 123, 2000, null);
        }
    }
}
